package com.care.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.care.dao.admin.impl.AdminDaoImpl;
import com.care.dao.user.impl.UserDaoImpl;
import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.care.entity.Page;
import com.care.entity.User;



@Service
@Transactional
public class UserServiceImpl {
	@Resource
	private UserDaoImpl udi;
	@Resource
	private AdminDaoImpl adi;
	
	//注册
	public void Register(String name, String email, String password, String sex) {
		if(adi.selUser(name)==null){
			udi.register(name, email, password, sex);
		}
	}
	
	//查询自己提问过的问题
	public Page<Consult> selAllConsult(User user,String number){
		int pageNum = 0;
		if(number==null || number.equals("")){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt(number);	
		}
		List<Consult> selAllConsult = udi.selAllConsult(user, pageNum, pageNum);
		int count = adi.selCountKnowledge();
		Page<Consult> p= new Page<Consult>(pageNum,4,count,selAllConsult);
		if((pageNum-1)==0){
			p.setPageNum(pageNum=1);
		}else {
			p.setPrePageNum(pageNum-1);
		}
		if((pageNum*4)>count){
			p.setNextPageNum(pageNum);
		}else{
			p.setNextPageNum(pageNum+1);
		}
		return p;
	}
	//添加问题
	public void addConsult(String content, String type, User user){
		if(user!=null){
			Consult consult = new Consult(content,type,user);
			udi.addConsult(consult);
		}
	}
	//删除问题
	public void delConsult(int id){
		Consult selConsult = udi.selConsult(id);
		udi.delConsult(selConsult);
	}
	//查看一个问题
	public Consult selConsult(int id){
		Consult consult = udi.selConsult(id);
		return consult;
	}
	//查看不同科室的问题
	public Page<Consult> selTypeConsult(String type,String number){
		int pageNum = 0;
		if(number==null || number.equals("")){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt(number);	
		}
		List<Consult> list = udi.selTypeConsult(type, pageNum, pageNum);
		int count = adi.selCountKnowledge();
		Page<Consult> p= new Page<Consult>(pageNum,4,count,list);
		if((pageNum-1)==0){
			p.setPageNum(pageNum=1);
		}else {
			p.setPrePageNum(pageNum-1);
		}
		if((pageNum*4)>count){
			p.setNextPageNum(pageNum);
		}else{
			p.setNextPageNum(pageNum+1);
		}
		return p;
		
	}
	//查看向某医生咨询的所有问题
	public List<Consult> selDocConsult(String doctorname){
		Doctor selDoctor = adi.selDoctor(doctorname);
		List<Consult> list = udi.selDocConsult(selDoctor);
		return list;
	}
	//更改自己的问题
	public void updConsult(int id,String content,String type , User user){
		if(user!=null){
			udi.updConsult(id, content, type);
		}
	}
	//改自己
	public boolean updUser(String name,String password,String sex,String eamil){
		if(udi.updUser(name, password, sex, eamil)){
			return true;
		}return false;		
	}
	/**
	 * 添加咨询
	 * @param consult
	 * @param doctor
	 * @param user
	 * @return 
	 */
	public boolean addConsultByUser(Consult consult,Doctor doctor,User user){
		return udi.addConsultByUser(consult, doctor, user);
	}
	/**
	 * 通过name查询全部信息
	 * @param name
	 * @return
	 */
	public Doctor findDoctorByName(String name){
		return udi.findDoctorByName(name);
		
	}
	/**
	  * 用户删除咨询
	  * @param consult
	  * @param user
	  */
	 public void deleteConsultByUser(Consult consult,User user){
		 udi.deleteConsultByUser(consult, user);
	 }
}
